package com.dmbjz.thymeleaf.controller;

import com.dmbjz.thymeleaf.entity.Person;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

@Controller
@RequestMapping("webSite")
@Slf4j
public class WebSiteController {

    @Resource
    private MessageSource messageSource;


    @GetMapping(value ="index")
    public ModelAndView index(){

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("website/index");
        return modelAndView;

    }

    @GetMapping(value ="header")
    public ModelAndView header(){

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("website/header");
        return modelAndView;

    }

    @GetMapping(value ="list")
    public ModelAndView list(){

        List<Person> personList = Arrays.asList(
                new Person(1L, "张三1", "100@163.com", 10, "pm"),
                new Person(2L, "张三2", "200@163.com", 20, "pm"),
                new Person(3L, "张三3", "300@163.com", 30, "pm"),
                new Person(4L, "张三4", "400@163.com", 40, "admin"),
                new Person(5L, "张三5", "500@163.com", 50, "boss"),
                new Person(6L, "张三6", "600@163.com", 60, "boss")
        );

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("personList",personList);
        modelAndView.setViewName("website/list");
        return modelAndView;

    }


    /** 代码方式获取国际化信息 */
    @GetMapping(value ="getMsgSource")
    public void getMsgSource(HttpServletRequest request){

        Locale locale = request.getLocale();
        //指定 messages.properties 中配置的国际化参数，获取信息
        String msg = messageSource.getMessage("sign", null, locale);
        log.info(msg);

    }

}
